using System;
using MDAControlsFramework;
using MDAControlsFramework.Helpers;

namespace MDACalendar.CalendarControls {
	class ControlMonth: MDAContainer {
		/// <summary>
		/// first day of month, which is showed
		/// </summary>
		private DateTime _firstDay;

		public DateTime FirstDay {
			get {
				return _firstDay;
			}
			set {
				this.DataChange(this, new MDADataChangeArgs(_firstDay, value));
				_firstDay = value;
			}
		}

		public override MDAMouseEventHandler MouseEventFactory(string name) {
			if (name == "movenext") {
				return this.MoveNextMonth;
			}

			return null;
		}

		public override void Initialize() {
			base.Initialize();
			FirstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
			SetMouseEvent("OnMouseSlideHorizontal", MoveNextMonth);
			SetMouseEvent("OnMouseSlideVertical", MoveNextYear);
		}

		public void MoveNextMonth(object sender, MDAMouseEventArgs e) {
			int difX = e.ActualPoint.X - e.FirstPoint.X;
			_animation.PrepareAnimation(this);

			if (difX < 0) {
				this.MoveNextMonth();
				_animation.AnimType = AnimationTypesEnum.rightToLeft;
			} else {
				this.MovePreviousMonth();
				_animation.AnimType = AnimationTypesEnum.leftToRight;
			}
			this.InvalidateGraphics();
			_animation.StartAnimate(this);
		}

		public void MoveNextYear(object sender, MDAMouseEventArgs e) {
			int difY = e.ActualPoint.Y - e.FirstPoint.Y;

			_animation.PrepareAnimation(this);
			if (difY < 0) {
				this.MoveNextYear();
				_animation.AnimType = AnimationTypesEnum.topToBottom;
			} else {
				this.MovePreviousYear();
				_animation.AnimType = AnimationTypesEnum.bottomToTop;
			}
			this.InvalidateGraphics();
			_animation.StartAnimate(this);
		}

		public void MoveNextMonth() {
			FirstDay = _firstDay.AddMonths(1);
		}

		public void MovePreviousMonth() {
			FirstDay = _firstDay.AddMonths(-1);
		}

		public void MoveNextYear() {
			FirstDay = _firstDay.AddYears(1);
		}

		public void MovePreviousYear() {
			FirstDay = _firstDay.AddYears(-1);
		}

		public void ShowDay(DateTime date) {
			_animation.PrepareAnimation(this);
			_animation.AnimType = AnimationTypesEnum.topToBottom;
			_parentControl = (MDAControl)MDASettings.GetObject("ControlDayList");
			_parentControl.InvalidateSize();
			_parentControl.InvalidateGraphics();
			_topForm.Invalidate();
			//this.Measure(_parentControl.Rectangle());
			
			//_animation.StartAnimate(_parentControl);
		}
	}
}
